package data;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class SaleAndBuyPool extends Thread{
	
	private static Map<String, StockPriceSaleAndBuy> sab = new HashMap<String, StockPriceSaleAndBuy>();

	public static StockPriceSaleAndBuy getSab(String code) {
		return sab.get(code);
	}
	
	public static void addCode(String code){
		if(sab.get(code)==null){
			StockPriceSaleAndBuy spasb = new StockPriceSaleAndBuy(code);
			sab.put(code, spasb);
		}
	}
	
	public static void removeCode(String code){
		sab.remove(code);
	}
	
	@Override
	public void run() {
		Set<String> codeset = LoadData.getStockList().keySet();
		for (String code : codeset) {
			addCode(code);
		}
		while(true){
			try {
				Thread.sleep(1000);
				Collection<StockPriceSaleAndBuy> values = sab.values();
				for (StockPriceSaleAndBuy stockPriceSaleAndBuy : values) {
					stockPriceSaleAndBuy.deal();
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
